package com.ruoyi.web.controller.ora;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.KeyValidate;
import com.ruoyi.common.enums.dict.SysYesNo;
import com.ruoyi.common.enums.dict.VoiceRecordType;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.ora.domain.Device;
import com.ruoyi.ora.domain.VoiceRecord;
import com.ruoyi.web.controller.common.CommonController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ora.domain.DeviceMessage;
import com.ruoyi.ora.service.IDeviceMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * app给设备发送的消息，设备端轮训获取Controller
 * 
 * @author liuwp
 * @date 2025-06-19
 */
@RestController
@RequestMapping("/ora/device_message")
public class DeviceMessageController extends BaseController
{

    private static final Logger log = LoggerFactory.getLogger(DeviceMessageController.class);

    @Autowired
    private IDeviceMessageService deviceMessageService;

    /**
     * 查询app给设备发送的消息，设备端轮训获取列表
     */
    @PreAuthorize("@ss.hasPermi('ora:device_message:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceMessage deviceMessage)
    {
        startPage();
        List<DeviceMessage> list = deviceMessageService.selectDeviceMessageList(deviceMessage);
        return getDataTable(list);
    }

    /**
     * 导出app给设备发送的消息，设备端轮训获取列表
     */
    @PreAuthorize("@ss.hasPermi('ora:device_message:export')")
    @Log(title = "app给设备发送的消息，设备端轮训获取", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceMessage deviceMessage)
    {
        List<DeviceMessage> list = deviceMessageService.selectDeviceMessageList(deviceMessage);
        ExcelUtil<DeviceMessage> util = new ExcelUtil<DeviceMessage>(DeviceMessage.class);
        util.exportExcel(response, list, "app给设备发送的消息，设备端轮训获取数据");
    }

    /**
     * 获取app给设备发送的消息，设备端轮训获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('ora:device_message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(deviceMessageService.selectDeviceMessageById(id));
    }

    /**
     * 新增app给设备发送的消息，设备端轮训获取
     */
    @PreAuthorize("@ss.hasPermi('ora:device_message:add')")
    @Log(title = "app给设备发送的消息，设备端轮训获取", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceMessage deviceMessage)
    {
        return toAjax(deviceMessageService.insertDeviceMessage(deviceMessage));
    }

    /**
     * 修改app给设备发送的消息，设备端轮训获取
     */
    @PreAuthorize("@ss.hasPermi('ora:device_message:edit')")
    @Log(title = "app给设备发送的消息，设备端轮训获取", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceMessage deviceMessage)
    {
        return toAjax(deviceMessageService.updateDeviceMessage(deviceMessage));
    }

    /**
     * 删除app给设备发送的消息，设备端轮训获取
     */
    @PreAuthorize("@ss.hasPermi('ora:device_message:remove')")
    @Log(title = "app给设备发送的消息，设备端轮训获取", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deviceMessageService.deleteDeviceMessageByIds(ids));
    }


    @GetMapping("/query")
    @KeyValidate()
    public AjaxResult query(String deviceSn)
    {
        log.info("查询设备消息：{}", deviceSn);
        DeviceMessage where = Builder.of(DeviceMessage::new)
                .with(DeviceMessage::setReadStatus, SysYesNo.NO.getValue())
                .with(DeviceMessage::setDeviceSn, deviceSn)
                .build();
        DeviceMessage deviceMessage = deviceMessageService.selectOneDeviceMessage(where);
        if (deviceMessage != null) {
            log.info("查询设备消息：{}，返回结果：{}", deviceSn, JSONObject.toJSONString(deviceMessage));
        }
        return AjaxResult.success(deviceMessage);
    }

    @GetMapping("/read")
    @KeyValidate()
    public AjaxResult read(Long messageId)
    {
        log.info("读取设备消息：{}", messageId);
        DeviceMessage where = Builder.of(DeviceMessage::new)
                .with(DeviceMessage::setReadStatus, SysYesNo.YES.getValue())
                .with(DeviceMessage::setReadTime, new Date())
                .with(DeviceMessage::setId, messageId)
                .build();
        int i = deviceMessageService.updateDeviceMessage(where);
        return toAjax(i > 0);
    }

}
